﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TGIApp.Web.Models;
using System.Collections;
using TGIApp.Model;

namespace TGIApp.Web.Controllers
{
    public class TicketController : Controller
    {
        //
        // GET: /Ticket/

        public ActionResult Index(int? event_id, int? venue_id, bool? Success)
        {
            var proxy = new Proxy.ServiceProxy();
            var events = proxy.GetAllEvents();
            var venues = proxy.GetAllVenues();
            ViewBag.Events = events;
            ViewBag.Venues = venues;
            if (Success.HasValue)
            {
                if (Success.Value)
                {
                    ViewBag.Title = "Operation completed successfully!";
                }
                else
                {
                    ViewBag.Title = "Operation could not be completed!";
                }

            }
            else
            {
                ViewBag.Title = "Index";
            }
            if (event_id.HasValue && venue_id.HasValue)
            {
                int eventId = event_id.Value;
                int venueId = venue_id.Value;
                var tickets = proxy.GetAllTicketsByEventIdAndVenueId(eventId, venueId);

                var sellTickets =
               from groupedTickets in
                   (from t in tickets
                    select new
                    {
                        AgentBranchId = t.AgentBranch_Id,
                        AgentBranchName = t.AgentBranch.Name,
                        EventId = t.TicketType.EventVenue.Event_Id,
                        EventVenueId = t.TicketType.EventVenue_Id,
                        ShowDateTime = t.EventDate,
                        ShowName = t.TicketType.EventVenue.ShowName,
                        TicketTypeClassName = t.TicketType.Class,
                        TicketTypeId = t.TicketType_Id,
                        VenueId = t.TicketType.EventVenue.Venue_Id,
                        CustomerId = t.Customer_Id,
                        TicketPrice = t.TicketType.Price,
                        AllotedTickets = t.TicketType.TicketQuotas.Where(q => q.AgentBranch_Id == t.AgentBranch_Id).FirstOrDefault().QuotaQuantity
                    })
               group groupedTickets by new
                   {
                       groupedTickets.AgentBranchId,
                       groupedTickets.AgentBranchName,
                       groupedTickets.EventId,
                       groupedTickets.EventVenueId,
                       groupedTickets.ShowDateTime,
                       groupedTickets.ShowName,
                       groupedTickets.TicketTypeClassName,
                       groupedTickets.TicketTypeId,
                       groupedTickets.TicketPrice,
                       groupedTickets.VenueId,
                       groupedTickets.AllotedTickets
                   } into g

               select new SellTicketViewModelForIndex()
               {
                   AgentBranchId = g.Key.AgentBranchId,
                   AgentBranchName = g.Key.AgentBranchName,
                   EventId = g.Key.EventId,
                   EventVenueId = g.Key.EventVenueId,
                   ShowDateTime = g.Key.ShowDateTime,
                   ShowName = g.Key.ShowName,
                   TicketTypeClassName = g.Key.TicketTypeClassName,
                   TicketTypeId = g.Key.TicketTypeId,
                   TicketPrice = g.Key.TicketPrice,
                   VenueId = g.Key.VenueId,
                   AllottedTickets = g.Key.AllotedTickets,
                   SoldTickets = g.Count(a => a.CustomerId.HasValue),
                   AvailableTickets = g.Key.AllotedTickets - g.Count(a => a.CustomerId.HasValue)
               };
                //
                return View(sellTickets);

            }
            else
            {
                return View();
            }

        }

        //
        // GET: /Ticket/Sell/agentBranchId=3&ticketTypeId=4&SellQty=16

        public ActionResult Sell(int? AgentBranchId, int? TicketTypeId, int? SellQty, decimal? TicketPrice)
        {
            ViewBag.Title = "Sell";
            var proxy = new Proxy.ServiceProxy();
            var customers = proxy.GetAllCustomers();
            var paymentModes = proxy.GetAllPaymentModes();
            ViewBag.Customers = customers;
            ViewBag.PaymentModes = paymentModes;
            var viewModel = new SellTicketViewModelForSubmit();
            if (AgentBranchId.HasValue && TicketTypeId.HasValue && SellQty.HasValue && TicketPrice.HasValue)
            {
                viewModel.AgentBranchId = AgentBranchId.Value;
                viewModel.TicketTypeId = TicketTypeId.Value;
                viewModel.SellQty = SellQty.Value;
                viewModel.TicketPrice = TicketPrice.Value;
                viewModel.Total = SellQty.Value * TicketPrice.Value;
                return View(viewModel);
            }
            else
            {
                return View();

            }
        }

        // post: /Ticket/Sell/agentBranchId=3&ticketTypeId=4&availableTickets=16
        [HttpPost]
        //public ActionResult Sell(int? agentBranchId, int? ticketTypeId, int? sellQty, List<Ticket> tickets)
        public ActionResult Sell(SellTicketViewModelForSubmit sellTicketViewModelForSubmit)
        {
            if (ModelState.IsValid)
            {
                ITgiService proxy = new Proxy.ServiceProxy();
                proxy.SellTickets(sellTicketViewModelForSubmit.TicketTypeId, sellTicketViewModelForSubmit.AgentBranchId, sellTicketViewModelForSubmit.CustomerId, sellTicketViewModelForSubmit.SellQty, sellTicketViewModelForSubmit.PaymentModeId, sellTicketViewModelForSubmit.Total);
            }
            return RedirectToAction("Index", new { Success= true});
            //ViewBag.Title = "Operation completed successfully!";
           // return View();
        }

    }
}
